<?php

/**
 * Execute view plugin
 *
 */
class Rapide_View_Helper_Plugin extends Rapide_View_Helper
{
	/**
	 * Execute view plugin
	 *
	 * @throws Rapide_View_Helper_Plugin_Exception if plugin is not dispatchable
	 * @param string $sPlugin
	 * @param string $sAction
	 * @param array $aParameters parameters for plugin
	 * @return string result of plugin
	 */
	public function execute($sPlugin, $sAction = 'Index', array $aParameters = array())
	{
		$oToken = new Rapide_Dispatcher_Token($sPlugin, $sAction);

		$oDispatcher = Rapide_Controller_Front::getInstance()
									 ->getDispatcher();

		if(!$oDispatcher->isDispatchable($oToken))
		{
			$sError = sprintf('Plugin %s (%s) is not dispatchable', $sPlugin, $sAction);
			throw new Rapide_View_Helper_Plugin_Exception();
		}
		
		$sControllersClass = $oDispatcher->formatControllerName($sPlugin);

		$oController = new $sControllersClass();

		// Merge current request's parameters with parameters given for plugin

		$aParameters = array_merge(Rapide_Controller_Front::getInstance()
															 ->getDispatcher()
															 ->getCurrentToken()
															 ->getParameters(),
															 $aParameters);

		$oController->initialize($aParameters);

		$sAction = $oDispatcher->formatActionName($sAction);

		// Plugin have to return the content of view, f.e. return $oView->fetch();

		return $oController->$sAction();
	}
}

?>